var $ = require("../../utils/Ajax.js");
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({
  data: {
    // 图片路径配置
    PictureUrl: getApp().PictureUrl,

    // 消息ID
    Id: null,

    // InitialTime
    InitialTime: 0,
 
    // 类型: 1为咨询文字      2为视频
    Type: null,

    // 默认视频播放时长
    HasTicket: 6,

    // 评论数据
    Comment: null,

    // 评论条数
    CommentLenght: 0,

    // 页面数据
    Information: null,

    // 用户评论内容
    CommentCont: null,

    // 底部弹出门票控制
    showShopPopup: false,
    animationData: {},

    UserInfor: null,
  },

  onLoad: function (options) {
    var that = this;
    that.setData({
      Id: options.Id,
      Type: options.Type
    })
    if (options.Type == 1){
      wx.setNavigationBarTitle({
        title: "资讯"
      }) 
    }else{
      wx.setNavigationBarTitle({
        title: "精彩回放"
      }) 
    }
    if (options.Type == 1){
      $.Ajax("api/information", "GET", {
        id: options.Id
      }, function (res) {
        if (res.s) {
          WxParse.wxParse('article', 'html', res.d.information.content, that, 0);
          that.setData({
            Comment: res.d.comment,
            CommentLenght: res.d.comment.length,
            Information: res.d.information
          })
          console.log(res.d)
        } else {
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      })
    }else{
      $.Ajax("api/videoInfo", "GET", {
        id: options.Id
      }, function (res) {
        if (res.s) {
          if (res.d.hasTicket != 0 || res.d.video.ticket == 0) {
            that.setData({
              HasTicket: 999999999999999999999999999999999999
            })
          }
          that.setData({
            Comment: res.d.video.comment,
            CommentLenght: res.d.video.comment.length,
            Information: res.d.video
          })
        } else {
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 3000,
            mask: true
          })
        }
      })
    }
  },

  onShow: function(){
    this.setData({
      UserInfor: wx.getStorageSync('UserInfor')
    })
  },

  // VideoTime
  VideoTime: function (e) {
    var VideoTime = e.detail.currentTime;
    if (VideoTime > Number(this.data.HasTicket)) {
      wx.createVideoContext('ZhangPeng').pause();
      wx.createVideoContext('ZhangPeng').seek(0);
      var animation = wx.createAnimation({
        duration: 400,
        timingFunction: "linear",
        delay: 0
      })
      this.animation = animation;
      animation.translateY(520).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: true
      })
      setTimeout(() => {
        animation.translateY(0).step()
        this.setData({
          animationData: animation.export()
        })
      }, 200)
    }
  },

  // 关闭购买门票
  MenPiaoBgClose: function (e) {
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation;
    animation.translateY(520).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showShopPopup: false
      })
    }.bind(this), 200)
  },

  // 监听用户输入内容
  CompleteComment: function(e){
    var CommentCont = e.detail.value.replace(/\s+/g, '');
    if (CommentCont.length > 0){
      this.setData({
        CommentCont: CommentCont
      })
    }else{
      this.setData({
        CommentCont: null
      })
    }
  },

  // 用户评论
  Comment: function(){
    var that = this;
    var CommentCont = that.data.CommentCont;
    if (CommentCont != null){
      $.Ajax("api/comment", "POST", {
        type: that.data.Type,
        objId: Number(that.data.Id),
        content: CommentCont
      }, function (res) {
        if (that.data.Type == 1) {
          $.Ajax("api/information", "GET", {
            id: that.data.Id
          }, function (res) {
            if (res.s) {
              that.setData({
                Comment: res.d.comment,
                CommentLenght: res.d.comment.length
              })
            }
          })
        } else {
          $.Ajax("api/videoInfo", "GET", {
            id: that.data.Id
          }, function (res) {
            if (res.s) {
              that.setData({
                Comment: res.d.video.comment,
                CommentLenght: res.d.video.comment.length,
              })
            }
          })
        }
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 3000,
          mask: true
        })
      })
    }else{
      wx.showToast({
        title: "评论内容不能为空",
        icon: 'none',
        duration: 3000,
        mask: true
      })
    }
  },

  // 用户打赏
  Gratuity: function(e){ 
    var Id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../Gratuity/Gratuity?Id=' + Id,
    })
  },

  // 用户点赞
  Praise: function(){
    var that = this;
    $.Ajax("api/userLaud","POST",{
      objId: Number(that.data.Id),
      type: that.data.Type
    },function(res){
      wx.showToast({
        title: res.m,
        icon: 'none',
        duration: 3000,
        mask: true
      })
    })
  },

  // 点击购买门票
  MenPiaoSubmit: function(){
    var that = this;
    var Ticket = that.data.Information.ticket;
    var balance = that.data.UserInfor.balance;
    if (Ticket > balance) {
      wx.showToast({
        title: '财富豆不足,请先充值',
        icon: 'success',
        duration: 2000
      })
      setTimeout(function(){
        wx.navigateTo({
          url: '../TopUpBeans/TopUpBeans',
        })
      },2000)
    } else {
      $.Ajax("api/ticket", "POST", {
        id: Number(that.data.Id),
        t: 0
      }, function (res) {
        that.MenPiaoBgClose();
        that.setData({
          HasTicket: 999999999999999999999999999999999999
        })
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500,
          mask: true
        })
      })
    }
  }
})